package com.ruoyi.web.controller.doctor;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FreeTime;
import com.ruoyi.system.service.IFreeTimeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 空闲时间(用于切片的)Controller
 * 
 * @author Wu Zhongfu
 * @date 2022-02-03
 */
@RestController
@RequestMapping("/doctor/free_time")
public class FreeTimeController extends BaseController
{
    @Autowired
    private IFreeTimeService freeTimeService;

    @Autowired
    private SysLoginService loginService;

    /**
     * 查询空闲时间(用于切片的)列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:free_time:list')")
    @GetMapping("/list")
    public TableDataInfo list(FreeTime freeTime)
    {
        startPage();
        List<FreeTime> list = freeTimeService.selectFreeTimeList(freeTime);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('doctor:free_time:listByUserId')")
    @GetMapping("/listByUserId")
    public TableDataInfo listByUserId(FreeTime freeTime)
    {
        startPage();
        freeTime.setUserId(getLoginUser().getUserId());
        List<FreeTime> list = freeTimeService.selectFreeTimeList(freeTime);
        return getDataTable(list);
    }

    /**
     * 导出空闲时间(用于切片的)列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:free_time:export')")
    @Log(title = "空闲时间(用于切片的)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FreeTime freeTime)
    {
        List<FreeTime> list = freeTimeService.selectFreeTimeList(freeTime);
        ExcelUtil<FreeTime> util = new ExcelUtil<FreeTime>(FreeTime.class);
        util.exportExcel(response, list, "空闲时间(用于切片的)数据");
    }

    /**
     * 获取空闲时间(用于切片的)详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(freeTimeService.selectFreeTimeById(id));
    }

    /**
     * 新增空闲时间(用于切片的)
     */
    @PreAuthorize("@ss.hasPermi('doctor:free_time:add')")
    @Log(title = "空闲时间(用于切片的)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FreeTime freeTime)
    {
        try {
            return toAjax(freeTimeService.insertFreeTime(freeTime));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改空闲时间(用于切片的)
     */
    @PreAuthorize("@ss.hasPermi('doctor:free_time:edit')")
    @Log(title = "空闲时间(用于切片的)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FreeTime freeTime)
    {
        try {
            return toAjax(freeTimeService.updateFreeTime(freeTime));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除空闲时间(用于切片的)
     */
    @PreAuthorize("@ss.hasPermi('doctor:free_time:remove')")
    @Log(title = "空闲时间(用于切片的)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(freeTimeService.deleteFreeTimeByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('doctor:free_time:remove')")
    @Log(title = "空闲时间(用于切片的)", businessType = BusinessType.DELETE)
    @DeleteMapping("/validateAndRmFreeTime/{ids}")
    public AjaxResult validateAndRmFreeTime(@PathVariable Integer[] ids,@RequestParam("pwd") String pwd)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean pwdRight = loginService.pwdRight(loginUser.getUsername(), pwd);
        if (!pwdRight) {
            return error("密码验证错误");
        }
        return toAjax(freeTimeService.deleteFreeTimeByIds(ids));
    }
}
